package cn.fintecher.gis.service;

import cn.fintecher.gis.entity.BusinessLayer;
import cn.fintecher.gis.entity.DistrictInfo;
import cn.fintecher.gis.entity.RestsFile;
import cn.fintecher.gis.entity.RestsInfo;
import cn.fintecher.gis.enums.CommonShow;
import cn.fintecher.gis.enums.FlowType;
import cn.fintecher.gis.enums.MessageType;
import cn.fintecher.gis.model.request.FlowApplyRequest;
import cn.fintecher.gis.model.request.LayerInfoCreateRequest;
import cn.fintecher.gis.model.request.RestsInfoRequest;
import cn.fintecher.gis.model.session.SessionModel;
import cn.fintecher.gis.utils.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("restsInfo")
public class RestsInfoService extends BaseService {

    /**
     * 其他项目信息的添加
     *
     * @param request
     * @param model
     * @throws Exception
     */
    public void add(RestsInfoRequest request, SessionModel model) throws Exception {
        RestsInfo restsInfo = new RestsInfo();
        BeanUtils.copyPropertiesIgnoreNull(request, restsInfo);
        restsInfo.setCreateTime(new Date());
        if (Objects.nonNull(request.getLayerBaseRequest())) {
            LayerInfoCreateRequest layerInfoCreateRequest = new LayerInfoCreateRequest();
            BeanUtils.copyPropertiesIgnoreNull(request.getLayerBaseRequest(), layerInfoCreateRequest);
            List<String> styleNames = new ArrayList<>();
            styleNames.add(request.getLayerBaseRequest().getStyleName());
            layerInfoCreateRequest.setStyleNames(styleNames);
            BusinessLayer businessLayer = getLayerInfo(layerInfoCreateRequest, FlowType.RESTS, model);
            restsInfo.setBusinessLayer(businessLayer);
        }
        Set<DistrictInfo> set = getDistrictInfoByIds(request.getRegions());
        restsInfo.setRegions(set);
        restsInfoRepository.save(restsInfo);
        if (Objects.nonNull(request.getFileIds()) && !request.getFileIds().isEmpty()) {
            List<RestsFile> list = new ArrayList<>();
            for (String fileId : request.getFileIds()) {
                RestsFile restsFile = new RestsFile();
                restsFile.setRestsId(restsInfo.getId());
                restsFile.setFileId(fileId);
                list.add(restsFile);
            }
            restsFileRepository.saveAll(list);
        }

        //添加流程申请
        FlowApplyRequest flowApplyRequest = new FlowApplyRequest();
        flowApplyRequest.setApplyId(restsInfo.getId());
        flowApplyRequest.setApplyUserId(model.getId());
        flowApplyRequest.setType(FlowType.RESTS);
        flowApplyRequest.setNextUserId(request.getNextUserId());
        if (request.getWhether().equals(CommonShow.YES)) {
            flowApplyRequest.setNextUserId(request.getNextUserId());
        }
        flowApplyRequest.setWhether(restsInfo.getWhether());
        flowService.flowApply(flowApplyRequest, MessageType.RESTS);
    }


    /**
     * 根据id查询对应的其他项目信息
     *
     * @param id
     * @return
     */
    public RestsInfo getInfoById(String id) {
        RestsInfo restsInfo = restsInfoRepository.findById(id).orElse(new RestsInfo());
        return restsInfo;
    }
}
